﻿/* San Antonio Health Center Patient Information Management System
 * 
 * =========================================
 * Description: Search Database
 * 
 * Functions:
 * -Account searching for family member
 * 
 * -----------------------------------------
 * Date Finished: February 22, 2010
 * Author: Mark Dominic M. De Mesa
 *       : Krismagne M. Maximo
 * =========================================
 * */

/**
 * =========================================
 * Updated by: Monica DM. Tosino
 * Update(s): Graphical User Interface
 * Date Finished: March 1, 2010
 * =========================================
 * **/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace SAHCPIMS
{
    public partial class SEARCH : Form
    {
        public int addMemberFlag;
        public string UpName;
        public int familyNumber;
        public int familyNumberSearched = 0;

        public int employeeNumber;
        public string employeeName;

        public SEARCH()
        {
            InitializeComponent();
        }

        public void putAccountInfo()
        {
            empNumLabel.Text = employeeNumber.ToString();
            employeeLabel.Text = employeeName;
        }

        private void logOutButton_Click(object sender, EventArgs e)
        {
            addMemberFlag = 0;
            LOGIN ac = new LOGIN();
            ac.Visible = true;
            this.Visible = false;
        }

        private void searchbutton_Click(object sender, EventArgs e)
        {
            string searchString =
                 @"Provider=Microsoft.Jet.OLEDB.4.0;"
                + @"Data source=..\\SAHCPIMS_database.mdb";

            string[] resultsArray = new string[10000];

            OleDbConnection conn = new OleDbConnection(searchString);
            DataSet ds = new DataSet();

            conn.Open();
            OleDbDataAdapter adapter =
            new OleDbDataAdapter("SELECT * from Family", conn);
            adapter.Fill(ds);
            conn.Close();

            DataTable dt = ds.Tables[0];
            string output = "";
            int counter = 0;


            foreach (DataRow dr in dt.Rows)
            {
                if ((textBox1.Text.ToLower() == (dr["LastName"].ToString().ToLower())))
                {
                    counter++;
                    output += "Family " + (dr["FamilyNum"]) + ":\n" + "Name: " + dr["LastName"].ToString() + ", " + dr["FirstName"].ToString() + " " + dr["MiddleName"].ToString() + "\n\n";

                }

                if (textBox1.Text.ToLower() == ((dr["FamilyNum"].ToString().ToLower())))
                {
                    counter++;
                    output += "Family " + (dr["FamilyNum"]) + ":\n" + "Name: " + dr["LastName"].ToString() + ", " + dr["FirstName"].ToString() + " " + dr["MiddleName"].ToString() + "\n" + "Birthdate: " + dr["Birthday"].ToString() + "\n" + "Address: " + dr["Address"].ToString() + "\n\n";
                    familyNumber = int.Parse(dr["FamilyNum"].ToString());
                    familyNumberSearched = 1;
                }


            }

            if (counter != 0)
            {
                if (familyNumberSearched == 1)
                {
                    if (addMemberFlag == 1)
                    {
                        addMemberFlag = 0;
                        ADDMEMBER addMemberForm = new ADDMEMBER();
                        addMemberForm.familyNumber = familyNumber;
                        addMemberForm.employeeName = employeeName;
                        addMemberForm.employeeNumber = employeeNumber;
                        addMemberForm.putAccountInfo();
                        addMemberForm.Visible = true;
                        this.Visible = false;

                    }
                }
                resultsLabel.Text = output;
                updatebutton.Visible = true;
                textBox2.Visible = true;
            }
            else
                resultsLabel.Text = "No Results Found!";
        }

        private void updatebutton_Click(object sender, EventArgs e)
        {
            UPDATE update = new UPDATE();
            update.upName = this.textBox2.Text;
            update.Visible = true;
            this.Visible = false;
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            HOME home = new HOME();
            home.homeEmpName = employeeName;
            home.homeEmpNum = employeeNumber;
            home.putAccountInfo();
            home.Visible = true;
            this.Visible = false;
        }

        
    }
}
